package cn.com.chec.en.controller.admin;

import cn.com.chec.en.dao.PageLinkDao;
import cn.com.chec.en.model.domain.PageLink;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({ "/admin/pageLink" })
public class PageLinkAdminController {
	protected static final Logger logger = Logger
			.getLogger(PageLinkAdminController.class);

	@Resource
	private PageLinkDao pageLinkDao;

	@RequestMapping({ "/list" })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		List<PageLink> list = this.pageLinkDao.getAllAsList();
		map.put("list", list);
		return new ModelAndView("admin/pageContent/pageLink", map);
	}

	@RequestMapping({ "/showUpdate" })
	public ModelAndView showUpdate(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		int id = StringUtils.isNotBlank(request.getParameter("id")) ? Integer
				.parseInt(request.getParameter("id")) : 0;
		PageLink pageLink = this.pageLinkDao.getLinkById(id);
		map.put("pageLink", pageLink);
		map.put("act", "update");
		return new ModelAndView("admin/pageContent/pageLinkOpr", map);
	}

	@RequestMapping({ "/update" })
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		int id = StringUtils.isNotBlank(request.getParameter("id")) ? Integer
				.parseInt(request.getParameter("id")) : 0;
		String linkValue = request.getParameter("linkValue");
		String message = "";
		if (this.pageLinkDao.updateLinkValuById(id, linkValue))
			message = "链接修改成功!";
		else {
			message = "链接修改失败!";
		}
		map.put("message", message);
		return new ModelAndView("admin/common/message", map);
	}
}